package com.geen.tuoke.ui.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.geen.tuoke.R
import com.geen.tuoke.base.BaseActivity
import com.geen.tuoke.bean.CustomerInfo
import com.geen.tuoke.databinding.ActivityAddLabelBinding
import com.geen.tuoke.ui.adapter.LabelAdapter
import com.geen.tuoke.ui.dialog.DialogAddTag
import com.geen.tuoke.ui.viewmodel.CustomerViewModel
import com.geen.tuoke.utils.ext.dp2px
import com.geen.tuoke.utils.ext.initMenu
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import kotlinx.android.synthetic.main.include_toolbar.*

/**
 * @Desc 添加标签
 */
class AddLabelActivity : BaseActivity<CustomerViewModel, ActivityAddLabelBinding>() {

    private val customerInfo: CustomerInfo by lazy {
        (intent.getSerializableExtra("customerInfo") as CustomerInfo)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_add_label
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        with(mBinding) {
            lifecycleOwner = this@AddLabelActivity
            initRV()
        }
        toolbar.initMenu("添加标签", onBack = {
            finish()
        }, rightImg = R.mipmap.icon_add) {
            DialogAddTag(this) { text ->
                reqAddLabel(text)
            }.showDialog()
        }


    }


    override fun initData() {

        reqLabelList()

        mViewModel.labelList.observe(this, {

            it.forEach {
                for (label in customerInfo.labels) {
                    if (it.id == label.id) {
                        it.isSelect = true
                    }
                }
            }
            adapter.setList(it)
        })
        mViewModel.addLabelResult.observe(this) {

            adapter.addData(it)
        }

    }

    fun updateCustomer(view: View) {
        val list = adapter.data.filter { it.isSelect }
        customerInfo.labels = list
        mViewModel.updateCustomer(customerInfo)
        finish()
    }

    private fun reqLabelList() {
        mViewModel.getLabelList()
    }

    private fun reqAddLabel(name: String) {
        mViewModel.addLabel(name)
    }

    lateinit var adapter: LabelAdapter
    private fun ActivityAddLabelBinding.initRV() {
        adapter = LabelAdapter()
        rv.layoutManager = GridLayoutManager(this@AddLabelActivity, 4)
        rv.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(
                this@AddLabelActivity
            ).size(dp2px(15f))
                .color(Color.TRANSPARENT).build()
        )
        rv.adapter = adapter
    }


    companion object {
        fun start(context: Context, customerInfo: CustomerInfo) {
            context.startActivity(Intent(context, AddLabelActivity::class.java).apply {
                putExtra("customerInfo", customerInfo)
            })
        }
    }


}